package edu.szte.ovrt.datahandler.ui.console;

import java.util.List;

import edu.szte.ovrt.datahandler.model.ModelBase;

public class Selector<T extends ModelBase> extends UIBase {
	private final List<T> m_list;

	public Selector(final List<T> list) {
		m_list = list;
	}

	protected void showMenu() {

		for (int i = 0; i < m_list.size(); i++) {
			showNumberedLine(i, m_list.get(i).getName());
		}
		showNumberedLine(m_list.size(), "Back to previous menu");
	}

	public final T getSelection() throws ExitAction {
		do {
			showMenu();
			final int selection = readInt();
			T item;
			if (selection == m_list.size()) { throw ExitAction.getInstance(); }
			if ((item = m_list.get(selection)) != null) { return item; }
		} while (true);
	}
}
